package com.hnucm.house.presenter;

import androidx.annotation.NonNull;

import com.hnucm.house.base.BasePresenter;
import com.hnucm.house.bean.NewHouseData;
import com.hnucm.house.contract.NewHouseCon;
import com.hnucm.house.model.NewHouseModel;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NewHousePresenter extends BasePresenter<NewHouseCon.INewHouseView> implements NewHouseCon.INewHousePresenter {

    NewHouseCon.INewHouseModel mINewHouseModel;

    public NewHousePresenter() {
        mINewHouseModel = new NewHouseModel();
    }

    @Override
    public void loadNewHouse() {
        mINewHouseModel.loadNewHouse()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<NewHouseData.DataDTO>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull List<NewHouseData.DataDTO> dataDTOS) {
                        if(isViewAttached()){
                            getView().onLoadNewHouse(dataDTOS);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        System.out.println(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refreshNewHouse() {

    }
}
